package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.dao.TakeoutMerchantCategoryDao;
import com.shop.admin.dao.TakeoutMerchantCategoryRelationDao;
import com.shop.admin.dao.TakeoutMerchantDao;
import com.shop.admin.entity.TakeoutMerchant;
import com.shop.admin.entity.TakeoutMerchantCategory;
import com.shop.admin.entity.TakeoutMerchantCategoryRelation;
import com.shop.admin.service.TakeoutMerchantService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.vo.TakeoutMerchantAndCategoryVo;
import com.shop.admin.vo.TakeoutMerchantUpdateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TakeoutMerchantServiceImpl extends ServiceImpl<TakeoutMerchantDao, TakeoutMerchant> implements TakeoutMerchantService {

    @Resource
    private TakeoutMerchantDao takeoutMerchantDao;

    @Resource
    private TakeoutMerchantCategoryRelationDao takeoutMerchantCategoryRelationDao;

    @Resource
    private TakeoutMerchantCategoryDao takeoutMerchantCategoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<TakeoutMerchant> page = new Page<>(Long.parseLong(params.get("page").toString()),Long.valueOf(params.get("limit").toString()) );
        Page<TakeoutMerchant> resPage=takeoutMerchantDao.pageTakeoutMerchantDesc(page,params);
        /**添加商家分类信息**/
        //查询分类关系表数据
        List<TakeoutMerchantCategoryRelation> takeoutMerchantCategoryRelationList = takeoutMerchantCategoryRelationDao.selectList(null);
        //查询分类表数据
        List<TakeoutMerchantCategory> takeoutMerchantCategories = takeoutMerchantCategoryDao.selectList(null);

        List<TakeoutMerchantAndCategoryVo> takeoutMerchantAndCategoryVos = resPage.getRecords().stream().map(takeoutMerchant -> {
            TakeoutMerchantAndCategoryVo takeoutMerchantAndCategoryVo = new TakeoutMerchantAndCategoryVo();
            BeanUtils.copyProperties(takeoutMerchant, takeoutMerchantAndCategoryVo);

            //分类关系表id与商家id进行匹配
            takeoutMerchantCategoryRelationList.forEach(relation -> {
                if (relation.getMerchantId().equals(takeoutMerchant.getId())) {
                    takeoutMerchantAndCategoryVo.setCateId(relation.getId().longValue());
                    takeoutMerchantAndCategoryVo.setCateName(relation.getCategoryName());
                }
            });
            //分类关系表分类id与分类表分类id进行匹配
            takeoutMerchantCategories.forEach(categories -> {
                if (categories.getCateId().equals(takeoutMerchantAndCategoryVo.getCateId())) {
                    takeoutMerchantAndCategoryVo.setCateUrl(categories.getCateUrl());
                }
            });
            return takeoutMerchantAndCategoryVo;
        }).collect(Collectors.toList());
        /**添加商家分类信息-end**/

        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(takeoutMerchantAndCategoryVos);
        return pageUtils;
    }

    @Override
    public void updateTakeoutMerchant(TakeoutMerchantUpdateVo vo) {
        TakeoutMerchant takeoutMerchant = new TakeoutMerchant();
        BeanUtils.copyProperties(vo,takeoutMerchant);
        takeoutMerchantDao.updateById(takeoutMerchant);
    }
}
